import SockJS from 'sockjs-client'
import Stomp from 'stompjs'
import { ElNotification } from 'element-plus'

let stompClient = null

export const connectSocket = () => {
  const socket = new SockJS('http://localhost:8080/ws-login') // 后端接口
  stompClient = Stomp.over(socket)

  stompClient.connect({}, () => {
    console.log('🟢 WebSocket 连接成功')

    // 订阅登录广播
    stompClient.subscribe('/topic/login', (message) => {
      const msg = message.body
      ElNotification({
        title: '用户登录通知',
        message: msg,
        position: 'bottom-right',
      })
    })
  })
}